package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.entity.ParcelCenter;
import io.geekidea.springbootplus.system.param.ParcelCenterPageParam;
import io.geekidea.springbootplus.system.service.ParcelCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 控制器
 *
 * @author miko
 * @since 2020-10-15
 */
@Slf4j
@RestController
@RequestMapping("/parcelCenter")
@Module("system")
@Api(value = "API", tags = {""})
public class ParcelCenterControllerExtra extends BaseController {

    @Autowired
    private ParcelCenterService parcelCenterService;

    /**
     * 分页列表
     */
    @PostMapping("/getBySchool")
    @OperationLog(name = "所有列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "所有列表", response = ParcelCenter.class)
    public ApiResult<List<ParcelCenter>> getAll(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        int schoolID = currentAdminInfo.getSchool().getSchoolID();
        LambdaQueryWrapper<ParcelCenter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParcelCenter::getSchoolID, schoolID).eq(ParcelCenter::getDeleted, 0);
        List<ParcelCenter> list = parcelCenterService.getAllList(wrapper);
        return ApiResult.ok(list);
    }

    /**
     * 添加
     */
    @PostMapping("/new")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addParcelCenter(@RequestBody JSONObject jsonObject, @CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        ParcelCenter parcelCenter = new ParcelCenter();
        parcelCenter.setName(jsonObject.getString("Name"));
        parcelCenter.setCreateTime(new Date());
        parcelCenter.setDeleted(0);
        parcelCenter.setSchoolID(currentAdminInfo.getSchool().getSchoolID());
        parcelCenter.setCreatePersonID(currentAdminInfo.getAdmin().getAdminID());
        try {
            boolean flag = parcelCenterService.save(parcelCenter);
        }catch (Exception e){
            parcelCenterService.update(parcelCenter, new LambdaQueryWrapper<ParcelCenter>().eq(ParcelCenter::getName, jsonObject.getString("Name")));
        }
        return ApiResult.result(true);
    }
}
